package com.hp.gui.jtree;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * TODO 注释
 *
 * @author hupan
 * @date 2019-11-20 14:13
 */
class ButtonCellEditor extends DefaultCellEditor {

    private static final long serialVersionUID = -6546334664166791132L;
    private JButton button;
    private JTable table;

    public ButtonCellEditor(JTable table) {
        super(new JTextField());
        this.setClickCountToStart(1);
        this.initButton();
        this.table = table;
    }

    private void initButton() {
        this.button = new JButton();
        this.button.setSize(50, 15);
        this.button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                ButtonCellEditor.this.fireEditingCanceled();
                System.out.println("Selected Column:"
                        + table.getSelectedColumn()
                        + ",row:"
                        + table.getSelectedRow()
                        + ",and filePath:"
                        + table.getModel()
                               .getValueAt(table.getSelectedRow(), 5));
            }
        });
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
                                                 boolean isSelected, int row, int column) {
        this.button.setText("下载");
        return this.button;
    }

    @Override
    public Object getCellEditorValue() {
        return this.button.getText();
    }
}

class ButtonCellRenderer implements TableCellRenderer {
    private JButton button;

    public ButtonCellRenderer() {
        this.button = new JButton();
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
                                                   boolean isSelected, boolean hasFocus, int row, int column) {
        this.button.setText("下载");
        return this.button;
    }

}